# -*- coding: utf-8 -*- 

from config import dbCon
from datetime import datetime


class Entree:
    def __init__(self, id=0, title=None, link=None, content=None, modified_date=None):
        self.id = id
        self.title = title
        self.link = link
        self.content = content
        self.modified_date = modified_date
        
    def add_entree(self, title, link, content):
        count = 0
        if title and link and content:
            results = dbCon.query("select count(*) as number from entree where entree_link = $v_entree_link", vars={'v_entree_link':link})
            if results[0].number:
                return count
            count = dbCon.insert('entree',
                                 entree_title=title,
                                 entree_link=link,
                                 entree_content=content,
                                 entree_modified_date=datetime.utcnow())
        return count
    
    def get_latest_entrees(self, offset=0, limit=10):
        result = list(dbCon.select('entree',
                              order="entree_modified_date desc",
                              offset = offset,
                              limit=limit+1)
                      )
        if Len(result)>limit:
            has_next = True
        return result[:limit], has_next
    
    def set_title(self, title):
        self.title = title
    def set_link(self, link):
        self.link = link
    def set_content(self, content):
        self.content = content
